home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / interp / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-18  |  3.4 KB  |  137 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: input.c,v 1.14 94/07/26 18:32:48 hallgren Exp $
  27. *
  28. * This file implements getc.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #include <sys/errno.h>
  37. #ifdef MACH
  38. extern void bzero(void *ptr, size_t bytes);
  39. extern int select(int nfds, fd_set *readfds, fd_set *write_fds,
  40.           fd_set *except_fds, struct timeval *timeout);
  41. #endif
  42. #if defined(__osf__) || defined(ultrix) || defined(sparc)
  43. extern void bzero(char *string, int length);
  44. extern int select(int nfds, fd_set *readfds, fd_set *writefds,
  45.           fd_set *exceptfds, struct timeval *timeout);
  46. #endif
  47. #ifdef sparc
  48. #include <errno.h>
  49. #endif
  50. #ifdef sgi
  51. #define pause buttplug
  52. #include <unistd.h>
  53. #undef pause
  54. #include <sys/types.h>
  55. #include <bstring.h>
  56. #include <sys/time.h>
  57. #include <errno.h>
  58. #endif
  59.  
  60. #include "mindy.h"
  61. #include "char.h"
  62. #include "list.h"
  63. #include "bool.h"
  64. #include "thread.h"
  65. #include "func.h"
  66. #include "driver.h"
  67. #include "error.h"
  68. #include "def.h"
  69.  
  70. static void getc_or_wait(struct thread *thread)
  71. {
  72.     if (
  73. #ifdef linux
  74.         stdin->_IO_read_ptr >= stdin->_IO_read_end
  75. #else
  76.         stdin->_cnt == 0
  77. #endif
  78.         && !feof(stdin)) {
  79.     int fd = fileno(stdin);
  80.     fd_set fds;
  81.     struct timeval tv;
  82.     int nfound;
  83.  
  84.     FD_ZERO(&fds);
  85.     FD_SET(fd, &fds);
  86.     tv.tv_sec = 0;
  87.     tv.tv_usec = 0;
  88. #ifdef hpux
  89.     nfound = select(fd+1, (int *)&fds, NULL, NULL, &tv);
  90. #else
  91.     nfound = select(fd+1, &fds, NULL, NULL, &tv);
  92. #endif
  93.  
  94.     if (nfound < 0) {
  95.         switch (errno) {
  96.           case EBADF:
  97.         error("Tried to getc with stdin broken.");
  98.           case EINTR:
  99.         wait_for_input(thread, fd, getc_or_wait);
  100.           case EINVAL:
  101.         lose("select failed with EINVAL?");
  102.         }
  103.     }
  104.     else if (nfound == 0)
  105.         wait_for_input(thread, fd, getc_or_wait);
  106.     }
  107.  
  108.     {
  109.     int c = getchar();
  110.     obj_t *old_sp = pop_linkage(thread);
  111.  
  112.     if (c != EOF)
  113.         *old_sp = int_char(c);
  114.     else
  115.         *old_sp = obj_False;
  116.  
  117.     thread->sp = old_sp + 1;
  118.  
  119.     do_return(thread, old_sp, old_sp);
  120.     }
  121. }
  122.  
  123. static obj_t dylan_getc(void)
  124. {
  125.     getc_or_wait(thread_current());
  126.     go_on();
  127.     /* go_on never returns. */
  128.     lose("go_on actually returned?");
  129.     return NULL;
  130. }
  131.  
  132. void init_input_functions(void)
  133. {
  134.     define_function("getc", obj_Nil, FALSE, obj_False, FALSE,
  135.             obj_CharacterClass, dylan_getc);
  136. }
  137.